package com.rc.saas.tenant.controller.hlht;


import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.client.ChargerClientRouter;
import com.rc.saas.tenant.common.base.HttpConstant;
import com.rc.saas.tenant.common.redis.RedisLoader;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.utils.StringUtil;
import com.rc.saas.tenant.common.utils.TimeUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.config.UpYunConfig;
import com.rc.saas.tenant.model.area.PlatformCity;
import com.rc.saas.tenant.model.area.PlatformProvince;
import com.rc.saas.tenant.model.discount.TenantDiscountPolicy;
import com.rc.saas.tenant.model.discount.TenantDiscountPolicyExample;
import com.rc.saas.tenant.model.discount.TenantDiscountStation;
import com.rc.saas.tenant.model.discount.TenantDiscountStationExample;
import com.rc.saas.tenant.model.elec.*;
import com.rc.saas.tenant.model.hlht.*;
import com.rc.saas.tenant.model.hlhtsk.*;
import com.rc.saas.tenant.model.operator.TenantOperator;
import com.rc.saas.tenant.model.operator.TenantOperatorStation;
import com.rc.saas.tenant.model.operator.TenantOperatorStationExample;
import com.rc.saas.tenant.model.order.OrderInfo;
import com.rc.saas.tenant.model.tenant.Tenant;
import com.rc.saas.tenant.model.tenant.TenantExample;
import com.rc.saas.tenant.model.tenant.TenantSetting;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.elec.ElecPileSearch;
import com.rc.saas.tenant.search.hlht.HlhtOrderSearch;
import com.rc.saas.tenant.search.hlht.HlhtTenantOpenSearch;
import com.rc.saas.tenant.search.hlht.HlhtTenantOpenStationSearch;
import com.rc.saas.tenant.service.area.PlatformAreaService;
import com.rc.saas.tenant.service.discount.TenantDiscountPolicyService;
import com.rc.saas.tenant.service.discount.TenantDiscountStationService;
import com.rc.saas.tenant.service.discount.TenantDiscountTimeService;
import com.rc.saas.tenant.service.elec.ElecGunService;
import com.rc.saas.tenant.service.elec.ElecPileService;
import com.rc.saas.tenant.service.elec.ElecStationPriceService;
import com.rc.saas.tenant.service.hlhtsk.*;
import com.rc.saas.tenant.service.operator.TenantOperatorStationService;
import com.rc.saas.tenant.service.order.OrderInfoService;
import com.rc.saas.tenant.service.tenant.TenantService;
import com.rc.saas.tenant.service.tenant.TenantSettingService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.util.JWTUtil;
import com.rc.saas.tenant.vo.discount.DiscountStationSaveVo;
import com.rc.saas.tenant.vo.discount.DiscountTimeSaveVo;
import com.rc.saas.tenant.vo.elec.ElecStationAndCityName;
import com.rc.saas.tenant.vo.elec.UnlockReq;
import com.rc.saas.tenant.vo.hlht.*;
import com.rc.saas.tenant.service.area.PlatformCityService;
import com.rc.saas.tenant.service.elec.ElecStationService;
import com.rc.saas.tenant.service.hlht.*;
import com.rc.saas.tenant.service.operator.TenantOperatorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/hlhtTenantOpenList")
public class HlhtTenantOpenListController {
    private static Logger logger = LoggerFactory.getLogger(HlhtTenantOpenListController.class);

    @Autowired
    private UpYunConfig upYunConfig;

    @Autowired
    private HlhtTenantOpenService hlhtTenantOpenService;

    @Autowired
    private HlhtskBizOperatorService hlhtskBizOperatorService;

    @Autowired
    private TenantOperatorService tenantOperatorService;

    @Autowired
    private HlhtskBizTenantService hlhtskBizTenantService;

    @Autowired
    private HlhtskGovtTenantOperatorService hlhtskGovtTenantOperatorService;//分销商对政府时，分销商的密钥Service

    @Autowired
    private HlhtskGovtTenantService hlhtskGovtTenantService;//分销商对政府时，设备商的密钥Service

    @Autowired
    private ElecStationService elecStationService;

    @Autowired
    private PlatformCityService platformCityService;

    @Autowired
    private HlhtOrderService hlhtOrderService;

    @Autowired
    HlhtTenantOpenStationService hlhtTenantOpenStationService;

    @Autowired
    private PlatformAreaService areaService;

    @Autowired
    private ElecStationPriceService elecStationPriceService;

    @Autowired
    private HlhtTenantOpenPriceService hlhtTenantOpenPriceService;

    @Autowired
    private TenantDiscountPolicyService tenantDiscountPolicyService;

    @Autowired
    private TenantDiscountTimeService tenantDiscountTimeService;

    @Autowired
    private TenantDiscountStationService tenantDiscountStationService;

    @Autowired
    private HlhtOperatorService hlhtOperatorService;

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private ChargerClientRouter chargerClientRouter;

    @Autowired
    private ElecPileService elecPileService;

    @Autowired
    private ElecGunService elecGunService;

    @Autowired
    private HlhtTenantOpenPileService hlhtTenantOpenPileService;

    @Autowired
    private HlhtTenantOpenGunService hlhtTenantOpenGunService;

    @Autowired
    private TenantOperatorStationService tenantOperatorStationService;

    @Autowired
    private HlhtskTenantService hlhtskTenantService;

    @Autowired
    private HlhtOperatorSettingService hlhtOperatorSettingService;

    @Autowired
    private TenantSettingService tenantSettingService;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private RedisLoader redisLoader;

    @Autowired
    private HlhtTenantOpenforService hlhtTenantOpenforService;

    /**
     * 跳转至 "互联互通列表"
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(BaseSearch baseSearch, Model model) {
        TenantUser user = ShiroUtils.getSessionUser();
        List<HlhtTenantVo> list = hlhtTenantOpenService.listHlhtTenantByTenantCode(user.getTenantCode());
        model.addAttribute("hlhtTenantList", list);
        model.addAttribute("tenantCode", baseSearch.getTenantCode());
        return "/templates/hlht/hlhtTenantOpen";
    }

    /**
     * 跳转互联订单列表
     *
     * @return
     */
    @RequestMapping(value = "/hlhtOrderInfo", method = RequestMethod.GET)
    public String hlhtOrderInfo(Model model, Long tenantOpenId, String tenantCode, String hlhtTenantCode, String hlhtOperatorId, String hlhtType) {
        HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(tenantOpenId);
        TenantUser user = ShiroUtils.getSessionUser();
        model.addAttribute("tenantCode", user.getTenantCode());
        model.addAttribute("hlhtTenantCode", hlhtTenantCode);
        model.addAttribute("hlhtOperatorId", hlhtOperatorId);
        model.addAttribute("tenantOpenId", tenantOpenId);
        model.addAttribute("hlhtTenantOpen", hlhtTenantOpen);
        model.addAttribute("hlhtType", hlhtType);
        model.addAttribute("hlhtOpenType", hlhtTenantOpen.getHlhtOpenType());
        return "/templates/hlht/hlhtOrderInfo";
    }

    /**
     * 跳转 "互联互通详情详情页面"页面
     *
     * @return
     */
    @RequestMapping(value = "/detailHlhtTenantOpen", method = RequestMethod.GET)
    public String detailHlhtTenantOpen(@RequestParam("tenantOpenId") Long tenantOpenId,
                                       @RequestParam("isEdit") String isEdit, Model model) {
        List<HlhtskGovtTenantOperatorAndTenantOperatorName> hlhtskGovtTenantOperatorList = new ArrayList<>();
        //互联互通基本信息
        HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(tenantOpenId);
        String tenantCode = hlhtTenantOpen.getTenantCode();
        String hlhtOperatorId = hlhtTenantOpen.getHlhtOperatorId();
        String hlhtTenantCode = hlhtTenantOpen.getHlhtTenantCode();

        if (BeanUtil.isValid(hlhtTenantOpen.getHlhtOpenType())) {
            if (hlhtTenantOpen.getHlhtOpenType().equals("2")) {//外部政府
                //分销商对政府时，获取运营商的密钥信息
                HlhtskGovtTenant hlhtskGovtTenant = hlhtTenantOpenService.selectHlhtskGovtTenant(tenantCode, hlhtOperatorId);
                //分销商与政府时，获取分销商的密钥信息列表
                hlhtskGovtTenantOperatorList =
                        hlhtTenantOpenService.listHlhtskGovtTenantOperator(tenantCode, hlhtOperatorId);
                model.addAttribute("hlhtskTenant", hlhtskGovtTenant);//以hlhtskTenant为对象传至前端
                model.addAttribute("hlhtskGovtTenantOperatorList", hlhtskGovtTenantOperatorList);
            } else if (hlhtTenantOpen.getHlhtOpenType().equals("1")) {//外部商业
                //设备运营商对第三方商业运营商时，获取设备运营商的密钥信息
                HlhtskBizTenant hlhtskBizTenant = hlhtTenantOpenService.selectHlhtskBizTenant(tenantCode, hlhtOperatorId);
                model.addAttribute("hlhtskTenant", hlhtskBizTenant);//以hlhtskTenant为对象传至前端
            } else if (hlhtTenantOpen.getHlhtOpenType().equals("0")) {//内部互联
                //内部互联设备运营商的密钥信息
                HlhtskTenant hlhtskTenant = hlhtTenantOpenService.selectHlhtskTenant(tenantCode, hlhtTenantCode);
                model.addAttribute("hlhtskTenant", hlhtskTenant);
            }
        }
        model.addAttribute("hlhtTenantOpen", hlhtTenantOpen);

        if (isEdit.equals("1")) {
            if (hlhtTenantOpen.getHlhtOpenType().equals("2")) {
                return "/templates/hlht/editHlhtTenantOpen";
            } else {
                return "/templates/hlhtsk/editHlhtTenantOpenAndBizsk";
            }
        }
        return "/templates/hlht/detailHlhtTenantOpen";
    }

    /**
     * 返回互联互通列表
     *
     * @return
     */
    @RequestMapping(value = "/listPageHlhtTenantOpen", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageHlhtTenantOpen(HlhtTenantOpenSearch hlhtTenantOpenSearch) {
        //当互联互通关系的结束时间小于当前时间，将该关系设置为过期状态
        hlhtTenantOpenService.updateStatusByExpiredDate();
        //查找过期的互联互通关系
        HlhtTenantOpenExample example = new HlhtTenantOpenExample();
        example.createCriteria().andStatusEqualTo("2");
        List<HlhtTenantOpen> list = hlhtTenantOpenService.selectByExample(example);
        //当互联关系过期，就将互联的站点下线
        for (HlhtTenantOpen hlhtTenantOpen : list) {
            if (hlhtTenantOpen.getHlhtType().equals("1") && (!"0".equals(hlhtTenantOpen.getHlhtOpenType()))) {
//                elecStationService.updateOperateStatusByHlhtTenantOpen(hlhtTenantOpen, "2");
            } else if (hlhtTenantOpen.getHlhtType().equals("0")) {
                hlhtTenantOpenStationService.updateOperateStatusByHlhtTenantOpen(hlhtTenantOpen, "2");
            }
        }
        PageInfo<HlhtTenantOpenVo> pageInfo = hlhtTenantOpenService.listPageHlhtTenantOpen(hlhtTenantOpenSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 导出 订单报表统计的相关订单
     */
    @ResponseBody
    @RequestMapping(value = "/exportHlhtTenantOpen", method = RequestMethod.GET)
    public TResponse exportHlhtTenantOpen(HlhtTenantOpenSearch hlhtTenantOpenSearch, HttpServletResponse response) {
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        ExportUtils.setDefaultPaging(hlhtTenantOpenSearch);
        hlhtTenantOpenSearch.setOffset(hlhtTenantOpenSearch.getLimit() * (hlhtTenantOpenSearch.getOffset() - 1));
        List<HlhtTenantOpenExportVo> list = hlhtTenantOpenService.listHlhtTenantOpen(hlhtTenantOpenSearch);
        if (list.size() > 150000) {
            return TResponse.FAIL("最多导出十五万数据");
        }

        if (list.size() >= 100000) {
            String exportData = (String) redisLoader.get("saas:exportData");
            if (exportData != null) {
                return TResponse.FAIL("导出人数过多,请稍后重试");
            }
            TenantUser user = ShiroUtils.getSessionUser();
            redisLoader.set("saas:exportData", user.getTenantCode(), 1000 * 20);
        }
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("互联关系列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), HlhtTenantOpenExportVo.class).sheet("互联关系列表").doWrite(list);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("导出互联关系列表失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

    /**
     * 更新互联互通的互联状态
     */
    @RequestMapping(value = "/updateHlhtTenantOpenStatus", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateHlhtTenantOpenStatus(Long tenantOpenId, String status) {
        try {
            HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(tenantOpenId);
            hlhtTenantOpen.setStatus(status);
            hlhtTenantOpenService.updateByPrimaryKeySelective(hlhtTenantOpen);
        } catch (Exception e) {
            logger.error("更新失败");
            return TResponse.FAIL("更新失败");
        }
        return TResponse.SUCCESS("更新成功");
    }


    /**
     * 更新互联互通设置和政府类密钥
     *
     * @param hlhtTenantOpenAndGovtsk
     * @return
     */
    @RequestMapping(value = "/updateHlhtTenantOpenAndGovtSk", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public TResponse updateHlhtTenantOpenAndGovtSk(@RequestBody HlhtTenantOpenAndGovtskVo hlhtTenantOpenAndGovtsk) {
        try {
            HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenAndGovtsk.getHlhtTenantOpen();
            HlhtskGovtTenant hlhtskGovtTenant = hlhtTenantOpenAndGovtsk.getHlhtskGovtTenant();
            List<HlhtskGovtTenantOperator> hlhtskGovtTenantOperators
                    = hlhtTenantOpenAndGovtsk.getHlhtskGovtTenantOperators();
            hlhtTenantOpenService.updateByPrimaryKeySelective(hlhtTenantOpen);
            hlhtskGovtTenantService.updateByPrimaryKeySelective(hlhtskGovtTenant);
            if (hlhtskGovtTenantOperators.size() > 0) {
                for (HlhtskGovtTenantOperator tenantOperator : hlhtskGovtTenantOperators) {
                    hlhtskGovtTenantOperatorService.updateByPrimaryKeySelective(tenantOperator);
                }
            }
        } catch (Exception e) {
            logger.error("更新失败", e);
            TResponse.FAIL("更新失败");
        }
        return TResponse.SUCCESS();
    }

    /**
     * 更新互联互通设置和商业类密钥
     *
     * @return
     */
    @RequestMapping(value = "/updateHlhtTenantOpenAndBizSk", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateHlhtTenantOpenAndBizSk(HlhtTenantOpen hlhtTenantOpen, HlhtskBizTenant hlhtskBizTenant) {
        try {
            hlhtTenantOpenService.updateByPrimaryKeySelective(hlhtTenantOpen);

            hlhtskBizTenantService.updateByPrimaryKeySelective(hlhtskBizTenant);
        } catch (Exception e) {
            logger.error("更新失败", e);
            TResponse.FAIL("更新失败");
        }
        return TResponse.SUCCESS();
    }

    @RequestMapping(value = "/deleteTenantOperatorSk", method = RequestMethod.POST)
    @ResponseBody
    public TResponse deleteTenantOperatorSk(Long tenantOperatorId) {
        try {
            hlhtskGovtTenantOperatorService.deleteByPrimaryKey(tenantOperatorId);
        } catch (Exception e) {
            logger.error("删除失败", e);
            TResponse.FAIL("删除失败");
        }
        return TResponse.SUCCESS("删除成功");
    }


    /**
     * 返回订单列表数据
     *
     * @return
     */
    /*@RequestMapping(value = "/listPageHlhtOrderInfo", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageHlhtOrderInfo(HlhtOrderSearch hlhtOrderSearch) {
        TenantUser user = ShiroUtils.getSessionUser();
        PageInfo<HlhtOrderVo> pageInfo = hlhtOrderService.listPageHlhtOrderInfo(hlhtOrderSearch, user);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }*/

    /**
     * 导出互联互通订单列表
     */
    /*@ResponseBody
    @RequestMapping(value = "/exportHlhtOrderInfo", method = RequestMethod.GET)
    public TResponse exportHlhtOrderInfo(HlhtOrderSearch hlhtOrderSearch, HttpServletResponse response) throws Exception {
        response.setHeader("Set-Cookie", "fileDownload=false; path=/");
        if (hlhtOrderSearch.getCount() == 0 || hlhtOrderSearch.getCount() == null) {
            hlhtOrderSearch.setCount(hlhtOrderSearch.getLimit());
        }
        if (hlhtOrderSearch.getCount() > 150000) {
            hlhtOrderSearch.setCount(150000);
            return TResponse.FAIL("最多导出15万数据");
        }

        //导出拼接开始条数
        hlhtOrderSearch.setOffset(hlhtOrderSearch.getLimit() * (hlhtOrderSearch.getOffset() - 1));
        List<HlhtOrderPrintVo> list = hlhtOrderService.listHlhtOrderInfo(hlhtOrderSearch);
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("订单列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), HlhtOrderPrintVo.class).sheet("订单列表").doWrite(list);
        } catch (Exception e) {
            logger.error("导出订单失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
        return TResponse.SUCCESS();
    }*/

    /**
     * 跳转站点开放管理页面(反向)
     *
     * @return
     */
    @RequestMapping(value = "/stationHlhtTenantOpen", method = RequestMethod.GET)
    public String stationHlhtTenantOpen(@RequestParam("tenantOpenId") Long tenantOpenId,
                                        @RequestParam("tenantCode") String tenantCode,
                                        Model model) {
        TenantUser user = ShiroUtils.getSessionUser();
        HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(tenantOpenId);
//        model.addAttribute("hlhtTenantOpen", hlhtTenantOpen.getHlhtTenantCode());
        model.addAttribute("hlhtTenantOpen", hlhtTenantOpen);
        List<PlatformCity> cities = platformCityService.listHlhtOpenStationCity(hlhtTenantOpen.getTenantCode());
        model.addAttribute("cities", cities);
        if (!user.getTenantCode().equals(hlhtTenantOpen.getTenantCode())) {
            return "/templates/hlht/stationHlhtTenantOpenPositive";//正方
        }

//        model.addAttribute("hlhtTenantOpen", hlhtTenantOpen);
        //开放类型,0 SAAS运营商    1商业运营平台
        if ("0".equals(hlhtTenantOpen.getHlhtOpenType())) {
            //反向互联,查询未互联的第三方运营商列表
            List<HlhtTenantVo> hlhtOperatoList = hlhtOperatorService.listAllHlhtTenantVo(tenantCode);
            model.addAttribute("hlhtOperatoList", hlhtOperatoList);
        } else {
            //显示saas运营商列表
            List<HlhtTenantVo> hlhtTenantList = hlhtTenantOpenService.listByHlhtTenantCode(hlhtTenantOpen.getTenantCode());
            model.addAttribute("hlhtTenantList", hlhtTenantList);
        }


        return "/templates/hlht/stationHlhtTenantOpen";
    }

    /**
     * 跳转至 站点开发管理页面(正向)
     *
     * @param tenantOpenId
     * @return
     */
    @RequestMapping(value = "/positiveHlhtStaion", method = RequestMethod.GET)
    public String positiveHlhtStation(Long tenantOpenId, Model model) {
        HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(tenantOpenId);
        model.addAttribute("hlhtTenantOpen", hlhtTenantOpen);
        List<PlatformCity> cities = platformCityService.listHlhtOpenStationCity(hlhtTenantOpen.getTenantCode());
        model.addAttribute("cities", cities);
        return "/templates/hlht/positiveHlhtStation";
    }


    /**
     * 返回站点开放管理列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageHlhtTenantOpenStation", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageHlhtTenantOpenStation(HlhtTenantOpenStationSearch hlhtTenantOpenStationSearch) {

        HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(hlhtTenantOpenStationSearch.getTenantOpenId());
        String hlhtOpenType = hlhtTenantOpen.getHlhtOpenType();
        String tenantCode = hlhtTenantOpen.getTenantCode();
        String hlhtType = hlhtTenantOpen.getHlhtType();
        String hlhtTenantCode = hlhtTenantOpen.getHlhtTenantCode();
        String hlhtOperatorId = hlhtTenantOpen.getHlhtOperatorId();
        PageInfo<HlhtTenantOpenStationVo> hlhtTenantOpenStationVoPageInfo = new PageInfo<>();
        hlhtTenantOpenStationSearch.setHlhtOpenType(hlhtOpenType);

        //互联关系为正向
        //目前的业务是只能通过设备运营商申请互联且关系是反向，所以当互联关系为正向时，说明站点数据是人工插入进来的
        //正向互联的站点数据是保存在表elec_station中且字段hlht_operator_id不为空，表hlht_tenant_open_station并没有数据
        if ("1".equals(hlhtType)) {
            hlhtTenantOpenStationVoPageInfo = elecStationService.listPageHlhtStation(hlhtTenantOpenStationSearch);
            return new WebPageInfo(hlhtTenantOpenStationVoPageInfo.getList(), Long.valueOf(hlhtTenantOpenStationVoPageInfo.getTotal()).intValue());
        }

        //由于拦截器会拦截登陆者ID和Search类中的tenantCode不一致的请求，所以用ownId代入sql查询中
        hlhtTenantOpenStationSearch.setOwnId(tenantCode);
        //内部
        if ("0".equals(hlhtOpenType)) {
            //说明当前系统用户是内部互联关系中的设备提供者
            if ("0".equals(hlhtType) && tenantCode.equals(hlhtTenantOpenStationSearch.getTenantCode())) {
                hlhtTenantOpenStationSearch.setHlhtTenantCode(hlhtTenantCode);
                hlhtTenantOpenStationVoPageInfo = elecStationService.listPageHlhtOpenStation(hlhtTenantOpenStationSearch);
            } else if ("0".equals(hlhtType) && (!tenantCode.equals(hlhtTenantOpenStationSearch.getTenantCode()))) {//当前系统用户是内部互联关系中的收取者
                hlhtTenantOpenStationSearch.setHlhtTenantCode(hlhtTenantCode);
                hlhtTenantOpenStationSearch.setIsOpen("1");//内部收取者的station记录只存在于hlht_tenant_open_station表中
                hlhtTenantOpenStationVoPageInfo = elecStationService.listPageHlhtOpenStation(hlhtTenantOpenStationSearch);
            }
        } else {//外部
            //说明当前系统用户是外部互联关系中的设备提供者
            if ("0".equals(hlhtType) && tenantCode.equals(hlhtTenantOpenStationSearch.getTenantCode())) {
                hlhtTenantOpenStationSearch.setHlhtOperatorId(hlhtOperatorId);
                hlhtTenantOpenStationVoPageInfo = elecStationService.listPageHlhtOpenStation(hlhtTenantOpenStationSearch);
            } else if ("0".equals(hlhtOpenType) && (!tenantCode.equals(hlhtTenantOpenStationSearch.getTenantCode()))) {//当前系统用户是外部互联关系中的收取者
                //hlhtTenantOpenStationSearch.setHlhtOperatorId(hlhtOperatorId);
                //hlhtTenantOpenStationSearch.setIsOpen("0");//不存在hlht_tenant_open_station表中
                //hlhtTenantOpenStationVoPageInfo = elecStationService.listPageHlhtOpenStation(hlhtTenantOpenStationSearch);
            }
        }
        WebPageInfo webPageInfo = new WebPageInfo(hlhtTenantOpenStationVoPageInfo.getList(), Long.valueOf(hlhtTenantOpenStationVoPageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    @ResponseBody
    @RequestMapping(value = "/exportHlhtTenantOpenStation", method = RequestMethod.GET)
    public TResponse exportHlhtTenantOpenStation(HlhtTenantOpenStationSearch hlhtTenantOpenStationSearch, HttpServletResponse response) {
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        ExportUtils.setDefaultPaging(hlhtTenantOpenStationSearch);
        HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(hlhtTenantOpenStationSearch.getTenantOpenId());
        String hlhtOpenType = hlhtTenantOpen.getHlhtOpenType();
        String tenantCode = hlhtTenantOpen.getTenantCode();
        String hlhtType = hlhtTenantOpen.getHlhtType();
        String hlhtTenantCode = hlhtTenantOpen.getHlhtTenantCode();
        String hlhtOperatorId = hlhtTenantOpen.getHlhtOperatorId();
        hlhtTenantOpenStationSearch.setHlhtOpenType(hlhtOpenType);

        List<HlhtTenantOpenStationExportVo> list = new ArrayList<HlhtTenantOpenStationExportVo>();

        //由于拦截器会拦截登陆者ID和Search类中的tenantCode不一致的请求，所以用ownId代入sql查询中
        hlhtTenantOpenStationSearch.setOwnId(tenantCode);
        //内部
        if ("0".equals(hlhtOpenType)) {
            //说明当前系统用户是内部互联关系中的设备提供者
            if ("0".equals(hlhtType) && tenantCode.equals(hlhtTenantOpenStationSearch.getTenantCode())) {
                hlhtTenantOpenStationSearch.setHlhtTenantCode(hlhtTenantCode);
                list = elecStationService.listHlhtTenantOpenStation(hlhtTenantOpenStationSearch);
                //= elecStationService.listPageHlhtOpenStation(hlhtTenantOpenStationSearch);
            } else if ("0".equals(hlhtType) && (!tenantCode.equals(hlhtTenantOpenStationSearch.getTenantCode()))) {//当前系统用户是内部互联关系中的收取者
                hlhtTenantOpenStationSearch.setHlhtTenantCode(hlhtTenantCode);
                hlhtTenantOpenStationSearch.setIsOpen("1");//内部收取者的station记录只存在于hlht_tenant_open_station表中
                list = elecStationService.listHlhtTenantOpenStation(hlhtTenantOpenStationSearch);
            }
        } else {//外部
            //说明当前系统用户是外部互联关系中的设备提供者
            if ("0".equals(hlhtType) && tenantCode.equals(hlhtTenantOpenStationSearch.getTenantCode())) {
                hlhtTenantOpenStationSearch.setHlhtOperatorId(hlhtOperatorId);
                list = elecStationService.listHlhtTenantOpenStation(hlhtTenantOpenStationSearch);
            } else if ("0".equals(hlhtOpenType) && (!tenantCode.equals(hlhtTenantOpenStationSearch.getTenantCode()))) {//当前系统用户是外部互联关系中的收取者
                //hlhtTenantOpenStationSearch.setHlhtOperatorId(hlhtOperatorId);
                //hlhtTenantOpenStationSearch.setIsOpen("0");//不存在hlht_tenant_open_station表中
                //hlhtTenantOpenStationVoPageInfo = elecStationService.listPageHlhtOpenStation(hlhtTenantOpenStationSearch);
            }
        }
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("站点列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), HlhtTenantOpenStationExportVo.class).sheet("站点列表").doWrite(list);
            //return TResponse.SUCCESS();
            //不需要再返回
            return null;
        } catch (Exception e) {
            logger.error("导出站点列表失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

    /**
     * 正向关系的站点分页列表
     */
    @ResponseBody
    @RequestMapping(value = "/listPagePositiveHlhtStation", method = RequestMethod.GET)
    public WebPageInfo listPagePositiveHlhtStation(HlhtTenantOpenStationSearch hlhtTenantOpenStationSearch) {
        PageInfo pageInfo = elecStationService.listPageHlhtStation(hlhtTenantOpenStationSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 批量操作互联站点
     *
     * @param openStationIdList
     * @param elecStationIdList
     * @param tenantOpenId
     * @param openStatus
     * @param hlhtOpenforType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateOrSaveHlhtTenantOpenStation", method = RequestMethod.POST)
    public TResponse updateOrSaveHlhtTenantOpenStation(@RequestParam("openStationIdList") List<Long> openStationIdList,
                                                       @RequestParam("elecStationIdList") List<Long> elecStationIdList,
                                                       @RequestParam("tenantOpenId") Long tenantOpenId,
                                                       @RequestParam("openStatus") String openStatus,
                                                       @RequestParam("hlhtOpenforType") String hlhtOpenforType,
                                                       @RequestParam("hlhtOperatorIds") List<String> hlhtOperatorIds) {

        TenantUser user = ShiroUtils.getSessionUser();
        HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(tenantOpenId);
        try {
            logger.info("批量操作互联站点updateOrSaveHlhtTenantOpenStation:" +
                    "openStationIdList:{},elecStationIdList:{},tenantOpenId:{},openStatus:{},hlhtOpenforType:{}", openStationIdList, elecStationIdList, tenantOpenId, openStatus, hlhtOpenforType);
            //已开放的站点list
            List<Long> openedElecStationIdList = new ArrayList<>();
            if (openStationIdList != null && openStationIdList.size() > 0) {
                openedElecStationIdList = hlhtTenantOpenStationService.getElecStationIdByOpenStation(openStationIdList);
            }
            //openStatus 0开放 1关闭
            if ("1".equals(openStatus) && openStationIdList != null && openStationIdList.size() > 0) {
                hlhtTenantOpenStationService.deleteByBatch(openStationIdList);
                hlhtTenantOpenPileService.deleteBatchByElecStationIdList(hlhtTenantOpen, openedElecStationIdList);
                hlhtTenantOpenGunService.deleteBatchByElecStationIdList(hlhtTenantOpen, openedElecStationIdList);

                //更改内部反向 第三方站点信息
                if (!StringUtil.isBlank(hlhtTenantOpen.getHlhtTenantCode())) {
                    hlhtTenantOpenStationService.deleteByBatchHlhtTenantOpen(elecStationIdList, hlhtTenantOpen, hlhtOpenforType);
                    hlhtTenantOpenforService.deleteByElecStationIds(elecStationIdList, hlhtTenantOpen.getTenantCode(), hlhtTenantOpen.getHlhtTenantCode());
                }

                if (!StringUtil.isBlank(hlhtTenantOpen.getHlhtOperatorId())) {
                    //关闭saas互联第三方表
                    hlhtTenantOpenforService.updateOpenforStatusByStationIds(elecStationIdList, hlhtTenantOpen.getHlhtOperatorId(), openStatus);
                }

                return TResponse.SUCCESS("关闭成功");
            }

            if (openedElecStationIdList != null && openedElecStationIdList.size() > 0) {
                //批量更新桩和枪的HlhtOpenforType
                hlhtTenantOpenPileService.updatePileHlhtOpenforTypeBatchByOpenStationIdList(hlhtTenantOpen, openedElecStationIdList, hlhtOpenforType);
                hlhtTenantOpenGunService.updateGunHlhtOpenforTypeBatchByOpenStationIdList(hlhtTenantOpen, openedElecStationIdList, hlhtOpenforType);

                //当初始设备方(如坤誉)不允许开放第三方时，中间方(如瑞晨)开放给第三方的站点的hlhtOpenforType也改为关闭
                if ("1".equals(hlhtOpenforType) && "0".equals(hlhtTenantOpen.getHlhtOpenType())) {
                    hlhtTenantOpenStationService.updateHlhtOpenforTypeByElecStationListAndHlhtOperatorNotNull(hlhtTenantOpen, openedElecStationIdList, hlhtOpenforType);
                }
            }

            if (elecStationIdList != null && elecStationIdList.size() > 0) {
                List<ElecStation> elecStationList = elecStationService.listByBatch(elecStationIdList);
                List<ElecPile> elecPileList = elecPileService.listElecPileByStationIdList(elecStationIdList);
                List<ElecGun> elecGunList = elecGunService.listElecGunByElecStationIdList(elecStationIdList);

                if (!StringUtil.isBlank(hlhtTenantOpen.getHlhtOperatorId())) {
                    //开启saas互联第三方表
                    hlhtTenantOpenforService.updateOpenforStatusByStationIds(elecStationIdList, hlhtTenantOpen.getHlhtOperatorId(), openStatus);
                }

                //先删除旧依赖关系
                hlhtTenantOpenStationService.deleteByBatchHlhtTenantOpen(elecStationIdList,hlhtTenantOpen,hlhtOpenforType);
                //批量插入hlhtTenantOpenStation
                hlhtTenantOpenStationService.insertByBatch(elecStationList, hlhtTenantOpen, hlhtOpenforType, user.getLoginName());
                //批量插入hlhtTenantOpenPile
                if (elecPileList != null && elecPileList.size() > 0) {
                    hlhtTenantOpenPileService.insertBatchByElecPileList(hlhtTenantOpen, elecPileList, hlhtOpenforType, user.getLoginName());
                }
                //批量插入hlhtTenantOpenGun
                if (elecGunList != null && elecGunList.size() > 0) {
                    hlhtTenantOpenGunService.insertBatchByElecGunList(hlhtTenantOpen, elecGunList, hlhtOpenforType, user.getLoginName());
                }

                //找出有申报资格的分销商站点
                TenantOperatorStationExample tenantOperatorStationExample = new TenantOperatorStationExample();
                tenantOperatorStationExample.createCriteria()
                        .andTenantCodeEqualTo(hlhtTenantOpen.getTenantCode())
                        .andGovtHlhtTypeEqualTo("1");
                List<TenantOperatorStation> tenantOperatorStationList = tenantOperatorStationService.selectByExample(tenantOperatorStationExample);

                //批量更新分销商
                if ("2".equals(hlhtTenantOpen.getHlhtOpenType())) {
                    hlhtTenantOpenStationService.updateTenantOperatorByStationId(tenantOperatorStationList);
                }
            }

            if ("0".equals(hlhtOpenforType) || (openStationIdList != null && openStationIdList.size() > 0)) {
                if ((openStationIdList == null || openStationIdList.size() == 0)) {
                    openStationIdList = hlhtTenantOpenStationService.selectOpenStationIdByhlhtTenantOpen(elecStationIdList, hlhtTenantOpen);
                }
                //批量更新站点的hlhtOpenforType
                hlhtTenantOpenStationService.updateHlhtOpenforTypeBatchByOpenStationIdList(openStationIdList, hlhtOpenforType);

                //hlhtOpenforType是否允许向第三方运营商开放 0 允许  1不允许  默认0
                if ("0".equals(hlhtOpenforType) && !CollectionUtils.isEmpty(hlhtOperatorIds) && !StringUtil.isBlank(hlhtTenantOpen.getHlhtTenantCode())) {
                    //批量新增反向运营商的第三方开放站点设置
                    hlhtTenantOpenforService.insertByElecStationIds(openStationIdList, hlhtTenantOpen.getTenantCode(), hlhtTenantOpen.getHlhtTenantCode(), hlhtOperatorIds);
                } else if ("0".equals(hlhtOpenforType) && CollectionUtils.isEmpty(hlhtOperatorIds) && StringUtil.isBlank(hlhtTenantOpen.getHlhtTenantCode())) {
                    //反向互联,第三方运营商列表
                    List<String> hlhtOperatoList = hlhtOperatorService.listAllHlhtTenantVo().stream().map(hlhtTenantVo -> hlhtTenantVo.getCode()).distinct().collect(Collectors.toList());
                    //批量新增反向运营商的所有第三方开放站点设置
                    hlhtTenantOpenforService.insertByElecStationIds(openStationIdList, hlhtTenantOpen.getTenantCode(), hlhtTenantOpen.getHlhtTenantCode(), hlhtOperatoList);
                } else if ("1".equals(hlhtOpenforType) && !StringUtil.isBlank(hlhtTenantOpen.getHlhtTenantCode())) {
                    //批量删除第三方开放站点设置
                    hlhtTenantOpenforService.deleteByHlhtElecStationIds(openStationIdList, hlhtTenantOpen.getTenantCode(), hlhtTenantOpen.getHlhtTenantCode());
                    //批量删除运营商与第三方站点互联关系
                    hlhtTenantOpenStationService.deleteByBatchHlhtOperateIds(openStationIdList, hlhtOperatorIds);
                }
            }
        } catch (Exception e) {
            logger.info("HlhtTenantOpenListController.updateOrSaveHlhtTenantOpenStation:{}", e);
            return TResponse.FAIL("操作失败");
        }
        return TResponse.SUCCESS();
    }

    /**
     * 批量上下线
     *
     * @param openStationOperateStatus
     * @return
     */
    @RequestMapping(value = "/updateBatchOpenStation", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateBatchOpenStation(@RequestParam("openStationIdList") List<Long> openStationIdList,
                                            @RequestParam("openStationOperateStatus") String openStationOperateStatus) {
        try {
            TenantUser user = ShiroUtils.getSessionUser();
            hlhtTenantOpenStationService.updateOperateStatusByBatch(openStationIdList, openStationOperateStatus, user.getLoginName());
        } catch (Exception e) {
            logger.error("操作失败", e);
            return TResponse.FAIL("操作失败");
        }
        return TResponse.SUCCESS("操作成功");
    }

    /**
     * 上下线
     *
     * @return
     */
    @RequestMapping(value = "/updateOpenStation", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateOpenStation(@RequestParam("openStationId") Long openStationId,
                                       @RequestParam("openStationOperateStatus") String openStationOperateStatus) {
        try {
//            HlhtTenantOpenStation hlhtTenantOpenStation = hlhtTenantOpenStationService.selectByPrimaryKey(openStationId);
//            hlhtTenantOpenStation.setOperateStatus(openStationOperateStatus);
//            hlhtTenantOpenStationService.updateByPrimaryKeySelective(hlhtTenantOpenStation);
            hlhtTenantOpenStationService.updateOperateStatusByOpenStationId(openStationId, openStationOperateStatus);
        } catch (Exception e) {
            logger.error("更新失败", e);
            return TResponse.FAIL("操作失败");
        }
        return TResponse.SUCCESS();
    }

    /**
     * 跳转站点详情
     *
     * @return
     */
    @RequestMapping(value = "/detailStation/{elecStationId}", method = RequestMethod.GET)
    public String detailStation(@PathVariable("elecStationId") Long elecStationId, String isEdit, Model model) {
        List<PlatformProvince> provinces = areaService.listAllProvince();
        ElecStationAndCityName elecStation = elecStationService.selectStationAndCity(elecStationId);
        TenantOperator tenantOperator = tenantOperatorService.selectByPrimaryKey(elecStation.getTenantOperatorCode());
        model.addAttribute("tenantOperator", tenantOperator);
        model.addAttribute("uploadPath", upYunConfig.getDownloadRoot());
        model.addAttribute("provinces", provinces);
        model.addAttribute("elecStation", elecStation);
        return "/templates/hlht/detailOpenStation";
    }


    /**
     * 修改批量互联站点的所有时间段的服务费调整数
     *
     * @param adjustValue
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateHlhtTenantOpenPriceForBatchOpenStation", method = RequestMethod.POST)
    public TResponse updateHlhtTenantOpenPriceForBatchOpenStation(@RequestParam("elecStationIdList") List<Long> elecStationIdList,
                                                                  @RequestParam("tenantOpenId") Long tenantOpenId,
                                                                  @RequestParam("adjustValue") BigDecimal adjustValue) {
        try {
            HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(tenantOpenId);
            List<HlhtTenantOpenPrice> hlhtTenantOpenPriceList = hlhtTenantOpenPriceService.selectByBatch(elecStationIdList, hlhtTenantOpen);
            hlhtTenantOpenPriceService.updateOperateAdjustValueByBatch(hlhtTenantOpenPriceList, adjustValue);
        } catch (Exception e) {
            logger.error("失败", e);
            return TResponse.FAIL("失败");
        }
        return TResponse.SUCCESS();
    }


    /**
     * 修改某个互联站点的所有时间段的服务费调整数
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateHlhtTenantOpenPriceForOpenStation", method = RequestMethod.POST)
    public TResponse updateHlhtTenantOpenPriceForOpenStation(Long elecStationId,
                                                             Long tenantOpenId,
                                                             BigDecimal adjustValue) {

        HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(tenantOpenId);
        HlhtTenantOpenPriceExample example = new HlhtTenantOpenPriceExample();
        HlhtTenantOpenPriceExample.Criteria criteria = example.createCriteria().andTenantCodeEqualTo(hlhtTenantOpen.getTenantCode());
        if ("0".equals(hlhtTenantOpen.getHlhtOpenType())) {
            criteria.andHlhtTenantCodeEqualTo(hlhtTenantOpen.getHlhtTenantCode());
        } else {
            criteria.andHlhtOperatorIdEqualTo(hlhtTenantOpen.getHlhtOperatorId());
        }
        criteria.andElecStationIdEqualTo(elecStationId);
        try {
            List<HlhtTenantOpenPrice> hlhtTenantOpenPriceList = hlhtTenantOpenPriceService.selectByExample(example);
            hlhtTenantOpenPriceService.updateOperateAdjustValueByBatch(hlhtTenantOpenPriceList, adjustValue);
        } catch (Exception e) {
            logger.error("fail", e);
            return TResponse.FAIL("失败");
        }
        return TResponse.SUCCESS();
    }

    /**
     * 修改某个互联站点的某些时间段的服务费调整数
     *
     * @param hlhtTenantOpenPriceList
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateHlhtTenantOpenPrice", method = RequestMethod.POST)
    public TResponse updateHlhtTenantOpenPrice(@RequestBody List<HlhtTenantOpenStationPriceVo> hlhtTenantOpenPriceList) {
        hlhtTenantOpenPriceService.updateHlhtTenantOpenPrice(hlhtTenantOpenPriceList);
        return TResponse.SUCCESS();
    }

    /**
     * 跳转至 互联互通开放站点价格
     *
     * @param elecStationId
     * @param tenantOpenId
     * @param model
     * @return
     */
    @RequestMapping(value = "/openPrice", method = RequestMethod.GET)
    public String openPrice(@RequestParam("elecStationId") Long elecStationId, @RequestParam("tenantOpenId") Long tenantOpenId, Model model) {

        HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(tenantOpenId);
        List<HlhtTenantOpenStationPriceVo> elecStationPriceList
                = elecStationPriceService.listStationPriceAndOpenPrice(hlhtTenantOpen.getTenantCode(), hlhtTenantOpen.getHlhtOpenType(),
                hlhtTenantOpen.getHlhtTenantCode(), hlhtTenantOpen.getHlhtOperatorId(),
                elecStationId);
        model.addAttribute("elecStationPriceList", elecStationPriceList);
        model.addAttribute("tenantOpenId", tenantOpenId);
        model.addAttribute("elecStationId", elecStationId);
        return "/templates/hlht/openPrice";
    }


    /**
     * 跳转互联互通折扣设置页面
     *
     * @return
     */
    @RequestMapping(value = "/hlhtDiscount", method = RequestMethod.GET)
    public String hlhtDiscount(@RequestParam("hlhtOperatorId") String hlhtOperatorId,
                               @RequestParam("tenantCode") String tenantCode,
                               @RequestParam("hlhtTenantCode") String hlhtTenantCode,
                               Model model) {

        if (StringUtil.isNotBlank(hlhtOperatorId) && !"null".equals(hlhtOperatorId)) {
            HlhtskBizOperatorExample example = new HlhtskBizOperatorExample();
            example.createCriteria().andTenantCodeEqualTo(tenantCode).andHlhtOperatorIdEqualTo(hlhtOperatorId).andHlhtTypeEqualTo("0");
            List<HlhtskBizOperator> hlhtskBizOperators = hlhtskBizOperatorService.selectByExample(example);

            if (!CollectionUtils.isEmpty(hlhtskBizOperators)) {
                HlhtskBizOperator hlhtskBizOperator = hlhtskBizOperators.get(0);
                model.addAttribute("hlhtskBizOperator", hlhtskBizOperator);
            }
            /**
             * tenantType:互联互通类型 :   0:内部 1:第三方
             */
            model.addAttribute("tenantType", "1");
        } else {
            HlhtskTenant hlhtskTenant = hlhtskTenantService.selectHlhtTenantCode(tenantCode, hlhtTenantCode);

            //Tenant tenant = tenantService.selectByPrimaryKey(hlhtTenantCode);

            HlhtskBizOperator hlhtskBizOperator = new HlhtskBizOperator();

            hlhtskBizOperator.setMemberId(hlhtskTenant.getHlhtMemberId());

            //hlhtsk_tenant.hlht_member_id:互联会员ID,属于tenant_code（由瑞晨平台方审核通过时自动创建，这个用户不能登录）,代表hlht_tenant_code,内部互联时需要
            //按设备商+运营商查找
            //tenantCode  设备方运营商
            //hlhtTenantCode SAAS内互联互通时对方运营商
            //HlhtskTenant hlhtskTenant = hlhtskTenantService.selectHlhtTenantCode(commandStopReq.getHlhtTenantCode(), commandStopReq.getTenantCode());

            hlhtskBizOperator.setTenantCode(tenantCode);

            model.addAttribute("hlhtskBizOperator", hlhtskBizOperator);

            model.addAttribute("tenantType", "0");
        }

        return "/templates/hlht/hlhtDiscount";

    }

    /**
     * 返回互联互通折扣列表
     *
     * @param baseSearch
     * @return
     */
    @RequestMapping(value = "/listPageHlhtDiscount", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageHlhtDiscount(BaseSearch baseSearch, String tenantCode, Long memberId) {
        PageInfo<HlhtDiscountResultVo> pageInfo = hlhtTenantOpenService.listPageHlhtDiscount(baseSearch, tenantCode, memberId);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 跳转增加互联互通折扣页面
     *
     * @return
     */
    @RequestMapping(value = "/addHlhtDiscount/{tenantCode}/{hlhtMemberId}/{tenantType}", method = RequestMethod.GET)
    public String addEnterpriseDiscount(@PathVariable("tenantCode") String tenantCode, @PathVariable("hlhtMemberId") Long hlhtMemberId,
                                        @PathVariable("tenantType") String tenantType, Model model) {
        model.addAttribute("tenantCode", tenantCode);
        model.addAttribute("hlhtMemberId", hlhtMemberId);
        model.addAttribute("tenantType", tenantType);

        if ("1".equals(tenantType)) {
            HlhtskBizOperatorExample hlhtskBizOperatorExample = new HlhtskBizOperatorExample();
            hlhtskBizOperatorExample.createCriteria().andMemberIdEqualTo(hlhtMemberId).andTenantCodeEqualTo(tenantCode).andHlhtTypeEqualTo("0");
            //查询反向对接的运营商
            List<HlhtskBizOperator> hlhtskBizOperators = hlhtskBizOperatorService.selectByExample(hlhtskBizOperatorExample);
            if (!CollectionUtils.isEmpty(hlhtskBizOperators)) {
                HlhtOperator hlhtOperator = hlhtOperatorService.selectByPrimaryKey(hlhtskBizOperators.get(0).getHlhtOperatorId());
                model.addAttribute("hlhtOperatorName", hlhtOperator.getHlhtOperatorName());
            }

        } else {

            HlhtskTenantExample hlhtskTenantExample = new HlhtskTenantExample();
            hlhtskTenantExample.createCriteria().andHlhtMemberIdEqualTo(hlhtMemberId).andTenantCodeEqualTo(tenantCode);
            List<HlhtskTenant> hlhtskTenants = hlhtskTenantService.selectByExample(hlhtskTenantExample);

            if (!CollectionUtils.isEmpty(hlhtskTenants)) {
                TenantExample tenantExample = new TenantExample();
                tenantExample.createCriteria().andTenantCodeEqualTo(hlhtskTenants.get(0).getHlhtTenantCode());
                List<Tenant> tenantList = tenantService.selectByExample(tenantExample);
                if (!CollectionUtils.isEmpty(tenantList)) {
                    model.addAttribute("hlhtOperatorName", tenantList.get(0).getTenantName());
                }
            }

        }

        return "/templates/hlht/addHlhtDiscount";
    }


    /**
     * 跳转编辑互联互通折扣页面
     *
     * @return
     */
    @RequestMapping(value = "/editHlhtDiscount/{discountStationId}/{tenantType}", method = RequestMethod.GET)
    public String editHlhtDiscount(@PathVariable("discountStationId") Long discountStationId,
                                   @PathVariable("tenantType") String tenantType, Model model) {
        TenantDiscountStation tenantDiscountStation = tenantDiscountStationService.selectByPrimaryKey(discountStationId);
        TenantDiscountPolicy tenantDiscountPolicy = tenantDiscountPolicyService.selectAllById(tenantDiscountStation.getDiscountPolicyId());

        if ("1".equals(tenantType)) {
            HlhtskBizOperatorExample hlhtskBizOperatorExample = new HlhtskBizOperatorExample();
            hlhtskBizOperatorExample.createCriteria().andMemberIdEqualTo(tenantDiscountPolicy.getHlhtMemberId()).andTenantCodeEqualTo(tenantDiscountPolicy.getTenantCode()).andHlhtTypeEqualTo("0");
            //查询反向对接的运营商
            List<HlhtskBizOperator> hlhtskBizOperators = hlhtskBizOperatorService.selectByExample(hlhtskBizOperatorExample);
            if (!CollectionUtils.isEmpty(hlhtskBizOperators)) {
                HlhtOperator hlhtOperator = hlhtOperatorService.selectByPrimaryKey(hlhtskBizOperators.get(0).getHlhtOperatorId());
                model.addAttribute("hlhtOperatorName", hlhtOperator.getHlhtOperatorName());
            }

        } else {
            TenantExample tenantExample = new TenantExample();
            tenantExample.createCriteria().andMemberIdEqualTo(tenantDiscountPolicy.getHlhtMemberId());
            List<Tenant> tenantList = tenantService.selectByExample(tenantExample);
            if (!CollectionUtils.isEmpty(tenantList)) {
                model.addAttribute("hlhtOperatorName", tenantList.get(0).getTenantName());
            }
        }

        model.addAttribute("tenantDiscountPolicy", tenantDiscountPolicy);
        model.addAttribute("discountStationId", tenantDiscountStation.getDiscountStationId());
        return "/templates/hlht/editHlhtDiscount";
    }


    /**
     * 保存互联互通折扣方案
     *
     * @return
     */
    @RequestMapping(value = "/saveHlhtDiscount", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveHlhtDiscount(Long discountPolicyId, String tenantCode, String discountType, String beginTime,
                                      String endTime, String remark, Long hlhtMemberId) {
        try {
            if (!BeanUtil.isValid(tenantCode, discountType, beginTime, endTime, remark)) {
                return TResponse.FAIL("请输入正确参数");
            }
            //优惠类型, 0服务费打折 1 特定服务费（活动价）
            TenantDiscountPolicy tenantDiscountPolicy = new TenantDiscountPolicy();
            //优惠政策名称为 企业名   优惠政策说明 备注
            tenantDiscountPolicy.setPolicyName("互联互通折扣");
            tenantDiscountPolicy.setPolicyDesc(remark);
            tenantDiscountPolicy.setDiscountType(discountType);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date begin = sdf.parse(beginTime);
            Date end = sdf.parse(endTime);
            tenantDiscountPolicy.setBeginTime(begin);

            tenantDiscountPolicy.setHlhtMemberId(hlhtMemberId);
            tenantDiscountPolicy.setEndTime(end);
            tenantDiscountPolicy.setStatus("0");
            tenantDiscountPolicy.setTenantCode(tenantCode);
            //优惠用户类型 0 全部 1指定用户 2企业用户  3.互联互通用户(反向外部互联)
            tenantDiscountPolicy.setPolicyMemberType("3");

            if (discountPolicyId == null) {
                tenantDiscountPolicyService.insertReturnKey(tenantDiscountPolicy);
            } else {
                tenantDiscountPolicy.setDiscountPolicyId(discountPolicyId);
                tenantDiscountPolicyService.updateByPrimaryKeySelective(tenantDiscountPolicy);
            }

            return TResponse.SUCCESS_DATA(tenantDiscountPolicy);
        } catch (Exception e) {
            logger.error("保存互联互通折扣方案失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 保存互联互通方案时段
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateHlhtDiscountTime", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateHlhtDiscountTime(@RequestBody String tenantDiscountTime) {
        TenantUser user = ShiroUtils.getSessionUser();

        try {
            String timeType = JSONObject.parseObject(tenantDiscountTime).getString("timeType");
            String discountType = JSONObject.parseObject(tenantDiscountTime).getString("discountType");
            Long discountPolicyId = JSONObject.parseObject(tenantDiscountTime).getLong("discountPolicyId");

            if (!BeanUtil.isValid(timeType, discountType, discountPolicyId)) {
                return TResponse.FAIL("请输入正确的参数");
            }

            //全天
            if ("1".equals(timeType)) {
                BigDecimal elecPrice = JSONObject.parseObject(tenantDiscountTime).getBigDecimal("elecPrice");
                BigDecimal servicePrice = JSONObject.parseObject(tenantDiscountTime).getBigDecimal("servicePrice");
                BigDecimal discount = JSONObject.parseObject(tenantDiscountTime).getBigDecimal("discount");

                tenantDiscountTimeService.insertDiscountTime(discountPolicyId, null, discountType, timeType, elecPrice, servicePrice, discount, null, user.getTenantCode());
            } else {
                List<DiscountTimeSaveVo> discountTimeSaveVoList = JSONObject.parseArray(JSONObject.parseObject(tenantDiscountTime).getString("discountTimeSaveVoList"), DiscountTimeSaveVo.class);

                List<String> list = new ArrayList<String>();
                for (DiscountTimeSaveVo discountTimeSaveVo : discountTimeSaveVoList) {
                    list.add(discountTimeSaveVo.getStartPeriod() + "-" + discountTimeSaveVo.getEndPeriod());
                }

                Boolean overlappTime = TimeUtil.isOverlappTime(list);

                if (overlappTime == true) {
                    return TResponse.FAIL("时间重叠");
                }

                tenantDiscountTimeService.insertDiscountTime(discountPolicyId, null, discountType, timeType, null, null, null, discountTimeSaveVoList, user.getTenantCode());
            }

            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存互联互通方案时段", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 保存互联互通折扣桩群
     *
     * @return
     */
    @RequestMapping(value = "/saveHlhtDiscountStation", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveHlhtDiscountStation(@RequestBody String hlhtDiscountStationList) {
        try {
            TenantUser user = ShiroUtils.getSessionUser();
            List<DiscountStationSaveVo> discountStationSaveVOS = JSON.parseArray(hlhtDiscountStationList, DiscountStationSaveVo.class);
            TResponse tResponse = tenantDiscountStationService.saveEnterpriseStation(user.getTenantCode(), discountStationSaveVOS);
            return tResponse;
        } catch (Exception e) {
            logger.error("保存互联互通折扣桩群失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 返回互联互通开放的所有桩群(已选/未选)
     *
     * @param hlhtMemberId
     * @param tenantCode
     * @param tenantType   互联互通类型 :   0:内部 1:第三方
     * @return
     */
    @RequestMapping(value = "/listStationByHlhtDiscountPolicy", method = RequestMethod.POST)
    @ResponseBody
    public TResponse listStationByEnterprisePolicy(Long hlhtMemberId, String tenantCode, String tenantType) {
        try {
            //返回结果列表
            ArrayList<ElecStation> elecStationList = new ArrayList<>();

            List<HlhtTenantOpenStation> hlhtTenantOpenStations = new LinkedList();

            if ("1".equals(tenantType)) {
                HlhtskBizOperatorExample hlhtskBizOperatorExample = new HlhtskBizOperatorExample();
                hlhtskBizOperatorExample.createCriteria().andMemberIdEqualTo(hlhtMemberId).andTenantCodeEqualTo(tenantCode).andHlhtTypeEqualTo("0");
                //查询反向对接的运营商
                List<HlhtskBizOperator> hlhtskBizOperators = hlhtskBizOperatorService.selectByExample(hlhtskBizOperatorExample);


//            HlhtTenantOpenStationExample hlhtTenantOpenStationExample = new HlhtTenantOpenStationExample();
//            hlhtTenantOpenStationExample.createCriteria().andTenantCodeEqualTo(tenantCode).andHlhtOperatorIdEqualTo(hlhtskBizOperators.get(0).getHlhtOperatorId());
//            List<HlhtTenantOpenStation> hlhtTenantOpenStations = hlhtTenantOpenStationService.selectByExample(hlhtTenantOpenStationExample);
                hlhtTenantOpenStations = hlhtTenantOpenStationService.listStationByEnterprisePolicy(tenantCode, hlhtskBizOperators.get(0).getHlhtOperatorId(), null);
            } else {

                HlhtskTenantExample hlhtskTenantExample = new HlhtskTenantExample();
                hlhtskTenantExample.createCriteria().andHlhtMemberIdEqualTo(hlhtMemberId).andTenantCodeEqualTo(tenantCode);
                List<HlhtskTenant> hlhtskTenants = hlhtskTenantService.selectByExample(hlhtskTenantExample);

                if (!CollectionUtils.isEmpty(hlhtskTenants)) {
                    TenantExample tenantExample = new TenantExample();
                    tenantExample.createCriteria().andTenantCodeEqualTo(hlhtskTenants.get(0).getHlhtTenantCode());
                    List<Tenant> tenantList = tenantService.selectByExample(tenantExample);
                    hlhtTenantOpenStations = hlhtTenantOpenStationService.listStationByEnterprisePolicy(tenantCode, null, tenantList.get(0).getTenantCode());
                }
            }

            if (CollectionUtils.isEmpty(hlhtTenantOpenStations)) {
                //没有开放站点返回null
                return null;
            }

            //查询是否存在方案
            TenantDiscountPolicyExample example = new TenantDiscountPolicyExample();
            example.createCriteria().andTenantCodeEqualTo(tenantCode).andHlhtMemberIdEqualTo(hlhtMemberId);
            //可能是多个方案
            List<TenantDiscountPolicy> tenantDiscountPolicyList = tenantDiscountPolicyService.selectByExample(example);

            ArrayList<Long> existStationDiscountList = new ArrayList<>();

            if (!CollectionUtils.isEmpty(tenantDiscountPolicyList)) {
                for (TenantDiscountPolicy tenantDiscountPolicy : tenantDiscountPolicyList) {
                    TenantDiscountStationExample tenantDiscountStationExample = new TenantDiscountStationExample();
                    tenantDiscountStationExample.createCriteria().andDiscountPolicyIdEqualTo(tenantDiscountPolicy.getDiscountPolicyId());
                    List<TenantDiscountStation> tenantDiscountStations = tenantDiscountStationService.selectByExample(tenantDiscountStationExample);
                    if (!CollectionUtils.isEmpty(tenantDiscountStations)) {
                        for (TenantDiscountStation tenantDiscountStation : tenantDiscountStations) {
                            existStationDiscountList.add(tenantDiscountStation.getElecStationId());
                        }
                    }
                }

                for (HlhtTenantOpenStation hlhtTenantOpenStation : hlhtTenantOpenStations) {
                    ElecStation elecStation = elecStationService.selectByPrimaryKey(hlhtTenantOpenStation.getElecStationId());
                    if (!CollectionUtils.isEmpty(existStationDiscountList)) {
                        for (Long aLong : existStationDiscountList) {
                            if (aLong.equals(elecStation.getElecStationId())) {
                                elecStation.setSelect("0");
                            }
                        }
                    } else {
                        elecStation.setSelect("1");
                    }

                    Tenant tenant = tenantService.selectByTenantCode(elecStation.getTenantCode());

                    elecStation.setOwnShortName(tenant.getTenantShortname());
                    elecStation.setOwnId(tenant.getTenantCode());

                    elecStationList.add(elecStation);
                }
                return TResponse.SUCCESS_DATA(elecStationList);
            } else {
                for (HlhtTenantOpenStation hlhtTenantOpenStation : hlhtTenantOpenStations) {
                    ElecStation elecStation = elecStationService.selectByPrimaryKey(hlhtTenantOpenStation.getElecStationId());
                    elecStation.setSelect("1");

                    Tenant tenant = tenantService.selectByTenantCode(elecStation.getTenantCode());

                    elecStation.setOwnShortName(tenant.getTenantShortname());
                    elecStation.setOwnId(tenant.getTenantCode());

                    elecStationList.add(elecStation);
                }
                return TResponse.SUCCESS_DATA(elecStationList);
            }
        } catch (Exception e) {
            logger.error("查询互联互通开放的失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 删除互联互通折扣桩群
     *
     * @param discountStationId
     * @return
     */
    @RequestMapping(value = "/deleteHlhtDiscountStation", method = RequestMethod.POST)
    @ResponseBody()
    public TResponse deleteHlhtDiscountStation(Long discountStationId) {
        try {
            TResponse tResponse = tenantDiscountPolicyService.deleteEnterpriseDiscountStation(discountStationId);
            return tResponse;
        } catch (Exception e) {
            logger.error("删除互联互通折扣桩群", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 批量删除互联互通折扣桩群
     *
     * @param discountStationList
     * @return
     */
    @RequestMapping(value = "/deleteBatchHlhtDiscountStation", method = RequestMethod.POST)
    @ResponseBody()
    public TResponse deleteBatchHlhtDiscountStation(@RequestParam List<Long> discountStationList) {
        try {
            TenantDiscountStationExample example = new TenantDiscountStationExample();
            example.createCriteria().andDiscountStationIdIn(discountStationList);
            tenantDiscountStationService.deleteByExample(example);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("批量删除互联互通桩群失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 编辑互联互通方案时段(新增方案)
     *
     * @return
     */
    @RequestMapping(value = "/saveEditHlhtDiscountTime", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveEditHlhtDiscountTime(@RequestBody String tenantDiscountTime) {
        TenantUser user = ShiroUtils.getSessionUser();

        try {
            String timeType = JSONObject.parseObject(tenantDiscountTime).getString("timeType");
            String discountType = JSONObject.parseObject(tenantDiscountTime).getString("discountType");
            Long discountPolicyId = JSONObject.parseObject(tenantDiscountTime).getLong("discountPolicyId");
            Long discountStationId = JSONObject.parseObject(tenantDiscountTime).getLong("discountStationId");


            if (!BeanUtil.isValid(timeType, discountType, discountPolicyId)) {
                return TResponse.FAIL("请输入正确的参数");
            }

            //全天
            if ("1".equals(timeType)) {
                BigDecimal elecPrice = JSONObject.parseObject(tenantDiscountTime).getBigDecimal("elecPrice");
                BigDecimal servicePrice = JSONObject.parseObject(tenantDiscountTime).getBigDecimal("servicePrice");
                BigDecimal discount = JSONObject.parseObject(tenantDiscountTime).getBigDecimal("discount");

                tenantDiscountTimeService.insertDiscountTime(discountPolicyId, null, discountType, timeType, elecPrice, servicePrice, discount, null, user.getTenantCode(), discountStationId);
            } else {
                List<DiscountTimeSaveVo> discountTimeSaveVoList = JSONObject.parseArray(JSONObject.parseObject(tenantDiscountTime).getString("discountTimeSaveVoList"), DiscountTimeSaveVo.class);

                List<String> list = new ArrayList<String>();
                for (DiscountTimeSaveVo discountTimeSaveVo : discountTimeSaveVoList) {
                    list.add(discountTimeSaveVo.getStartPeriod() + "-" + discountTimeSaveVo.getEndPeriod());
                }

                Boolean overlappTime = TimeUtil.isOverlappTime(list);

                if (overlappTime == true) {
                    return TResponse.FAIL("时间重叠");
                }

                tenantDiscountTimeService.insertDiscountTime(discountPolicyId, null, discountType, timeType, null, null, null, discountTimeSaveVoList, user.getTenantCode(), discountStationId);
            }

            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存互联互通方案时段失败", e);
            return TResponse.FAIL("操作失败");
        }
    }

    /**
     * 跳转至补推订单
     */
    @RequestMapping(value = "/updateOrder", method = RequestMethod.GET)
    public String updateOrder() {
        return "/templates/hlht/updateOrder";
    }

    /**
     * 补推订单
     */
    @ResponseBody
    @RequestMapping(value = "/reUpdateOrder", method = RequestMethod.POST)
    public TResponse reUpdateOrder(@RequestBody List<String> outOrderNumList) {
        logger.info("补推订单");
        for (String outOrderNum : outOrderNumList) {
            OrderInfo orderInfo = orderInfoService.selectOrderInfoByOutOrderNum(outOrderNum);

            if (orderInfo != null) {
                String tenantCode = orderInfo.getTenantCode();
                HlhtskTenant hlhtskTenant = hlhtskTenantService.selectByTenantCode(tenantCode);

                //使用运营商的data_secret加密(token有效期:3分钟)
                String jwt = JWTUtil.generateToken(tenantCode, hlhtskTenant.getDataSecret());

                ElecStation elecStation = elecStationService.selectByPrimaryKey(orderInfo.getElecStationId());
                logger.info("orderId:{},orderNum:{},outOrderNum:{},domain:{},domainSpliter:{},tenantCode:{},dataSecret:{},jwt:{}", orderInfo.getOrderId(), orderInfo.getOrderNum(), orderInfo.getOutOrderNum(),
                        elecStation.getElecServerDomain(), elecStation.getElecServerSpliter(), tenantCode, hlhtskTenant.getDataSecret(), jwt);
                TResponse tResponse = chargerClientRouter.updateOrderCostHlht1(elecStation.getElecServerDomain(), elecStation.getElecServerSpliter(),
                        tenantCode, orderInfo.getOrderId(), jwt);
                logger.info("补推orderNum:{},result:{},message:{},data:{}", orderInfo.getOrderNum(), tResponse.getStatus(), tResponse.getMessage(), tResponse.getData());

                if (tResponse.getStatus() != 200) {
                    logger.info("补退失败:{},orderNum:{},outOrderNum:{}", tResponse.getMessage(), orderInfo.getOrderNum(), orderInfo.getOutOrderNum());
                    return TResponse.FAIL("补退失败:" + outOrderNum);
                }
                logger.info("补推成功--orderNum:{},outOrderNum:{},message:{},data:{}", orderInfo.getOrderNum(), orderInfo.getOutOrderNum(), tResponse.getMessage(), tResponse.getData());
            }

        }
        return TResponse.SUCCESS();
    }

    /**
     * 跳转至 正向关系站点详情
     */
    @RequestMapping(value = "/positiveHlhtStationDetail/{elecStationId}", method = RequestMethod.GET)
    public String positiveHlhtStationDetail(@PathVariable("elecStationId") Long elecStationId, Model model) {
        List<PlatformProvince> provinces = areaService.listAllProvince();
        ElecStationAndCityName elecStation = elecStationService.selectPositiveHlhtStationAndCity(elecStationId);
        TenantOperator tenantOperator = tenantOperatorService.selectByPrimaryKey(elecStation.getTenantOperatorCode());
        model.addAttribute("tenantOperator", tenantOperator);
        model.addAttribute("uploadPath", upYunConfig.getDownloadRoot());
        model.addAttribute("provinces", provinces);
        model.addAttribute("elecStation", elecStation);
        return "/templates/elec/detailStation";
    }

    /**
     * 跳转至 正向关系站点编辑页
     */
    @RequestMapping(value = "/editPositiveHlhtStation/{elecStationId}", method = RequestMethod.GET)
    public String editPositiveHlhtStation(@PathVariable("elecStationId") Long elecStationId, Model model) {
        TenantUser user = ShiroUtils.getSessionUser();
        List<TenantOperator> tenantOperators = tenantOperatorService.listOperatorByTenantCode(user.getTenantCode());
        model.addAttribute("tenantOperators", tenantOperators);
        List<PlatformProvince> provinces = areaService.listAllProvince();
        ElecStationAndCityName elecStation = elecStationService.selectPositiveHlhtStationAndCity(elecStationId);
        model.addAttribute("uploadPath", upYunConfig.getDownloadRoot());
        model.addAttribute("provinces", provinces);
        model.addAttribute("elecStation", elecStation);
        return "/templates/elec/editElecStation";
    }

    /**
     * 正向关系中的站点批量上下线
     */
    @RequestMapping(value = "/updateBatchPositiveHlhtStation", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateBatchPositiveHlhtStation(@RequestParam("elecStationIdList") List<Long> elecStationIdList,
                                                    @RequestParam("operateStatus") String operateStatus) {
        try {
            elecStationService.updateBatchPositiveHlhtStation(elecStationIdList, operateStatus);
            Integer status = new Integer(1);
            if (!"1".equals(operateStatus)) {
                status = null;
            }
            elecPileService.updateBatchElecPileOperateStatusByElecStationIdList(elecStationIdList, operateStatus, status);
            elecGunService.updateBatchElecGunOperateStatusByElecStationIdList(elecStationIdList, operateStatus);
        } catch (Exception e) {
            logger.error("操作失败", e);
            return TResponse.FAIL("操作失败");
        }
        return TResponse.SUCCESS("操作成功");
    }

    /**
     * 跳转至 桩列表
     *
     * @return
     */
    @RequestMapping(value = "/pileList/{elecStationId}", method = RequestMethod.GET)
    public String pileList(@PathVariable("elecStationId") Long elecStationId, Model model) {
        model.addAttribute("elecStationId", elecStationId);
        return "/templates/hlht/pileList";
    }

    /**
     * 返回 互联桩列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listPageElecPile", method = RequestMethod.GET)
    public WebPageInfo listPageElecPile(ElecPileSearch elecPileSearch) {
        PageInfo<ElecPile> pageInfo = elecPileService.listPageElecPile(elecPileSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 解锁枪功能
     *
     * @param elecGunId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "sendUnlock", method = RequestMethod.POST)
    public TResponse sendUnlock(Long elecGunId) {
        try {
            ElecGun elecGun = elecGunService.selectByPrimaryKey(elecGunId);
            ElecStation joinElecStation = elecStationService.selectByPrimaryKey(elecGun.getElecStationId());

            UnlockReq unlockReq = new UnlockReq();
            unlockReq.setPileId(elecGun.getElecPileId());
            unlockReq.setGunNum(elecGun.getGunNum());

            TResponse tResponse = chargerClientRouter.sendUnlock(unlockReq);
            if (tResponse.getStatus() == HttpConstant.STATUS_SUCCESS) {
                return tResponse;
            } else {
                return TResponse.FAIL("调用解锁接口失败");
            }

            //List<NameValuePair> list = new ArrayList<>();
            //list.add(new BasicNameValuePair("pileId", String.valueOf(elecGun.getElecPileId())));
            //list.add(new BasicNameValuePair("gunNum", elecGun.getGunNum()));
            //
            //return Request.Post(serverUrl + "/command/sendUnlock").bodyForm(list, Charset.forName("UTF-8")).execute()
            //        .handleResponse(new ResponseHandler<TResponse>() {
            //            @Override
            //            public TResponse handleResponse(HttpResponse httpResponse) throws IOException {
            //                int code = httpResponse.getStatusLine().getStatusCode();
            //                if (code == 200) {
            //                    String result = EntityUtils.toString(httpResponse.getEntity());
            //                    logger.info("解锁返回:" + result);
            //                    return new Gson().fromJson(result, TResponse.class);
            //                } else {
            //                    logger.error(EntityUtils.toString(httpResponse.getEntity()));
            //                    return TResponse.FAIL("调用解锁接口失败");
            //                }
            //            }
            //        });
        } catch (Exception e) {
            logger.error("发送解锁指令失败", e);
            return TResponse.FAIL("发送解锁指令失败");
        }
    }


    @RequestMapping(value = "/synchronizeHlhtStationPrice", method = RequestMethod.POST)
    public TResponse synchronizeHlhtStationPrice() {
        TenantUser user = ShiroUtils.getSessionUser();
        //找出与该运营商有关的反向互联关系
        HlhtTenantOpenExample hlhtTenantOpenExample = new HlhtTenantOpenExample();
        hlhtTenantOpenExample.createCriteria().andTenantCodeEqualTo(user.getTenantCode()).andHlhtTypeEqualTo("0");
        List<HlhtTenantOpen> hlhtTenantOpenList = hlhtTenantOpenService.selectByExample(hlhtTenantOpenExample);

        //找出与该运营商有关的所有站点价格
        ElecStationPriceExample elecStationPriceExample = new ElecStationPriceExample();
        elecStationPriceExample.createCriteria().andTenantCodeEqualTo(user.getTenantCode());
        List<ElecStationPrice> elecStationPriceList = elecStationPriceService.selectByExample(elecStationPriceExample);

        for (HlhtTenantOpen hlhtTenantOpen : hlhtTenantOpenList) {
            for (ElecStationPrice elecStationPrice : elecStationPriceList) {
                HlhtTenantOpenPriceExample hlhtTenantOpenPriceExample = new HlhtTenantOpenPriceExample();
                HlhtTenantOpenPriceExample.Criteria criteria = hlhtTenantOpenPriceExample.createCriteria()
                        .andTenantCodeEqualTo(hlhtTenantOpen.getTenantCode())
                        .andElecStationIdEqualTo(elecStationPrice.getElecStationId())
                        .andPriceIdEqualTo(elecStationPrice.getPriceId());
                if (hlhtTenantOpen.getHlhtTenantCode() != null) {
                    criteria.andHlhtTenantCodeEqualTo(hlhtTenantOpen.getHlhtTenantCode());
                }
                if (hlhtTenantOpen.getHlhtOperatorId() != null) {
                    criteria.andHlhtOperatorIdEqualTo(hlhtTenantOpen.getHlhtOperatorId());
                }
                long count = hlhtTenantOpenPriceService.countByExample(hlhtTenantOpenPriceExample);
                if (count > 0) {
                    continue;
                }
                HlhtTenantOpenPrice hlhtTenantOpenPrice = new HlhtTenantOpenPrice();
                hlhtTenantOpenPrice.setTenantCode(hlhtTenantOpen.getTenantCode());
                hlhtTenantOpenPrice.setHlhtTenantCode(hlhtTenantOpen.getHlhtTenantCode());
                hlhtTenantOpenPrice.setHlhtOperatorId(hlhtTenantOpen.getHlhtOperatorId());
                hlhtTenantOpenPrice.setPriceId(elecStationPrice.getPriceId());
                hlhtTenantOpenPrice.setElecStationId(elecStationPrice.getElecStationId());
                try {
                    hlhtTenantOpenPriceService.insertSelective(hlhtTenantOpenPrice);
                } catch (Exception e) {
                    logger.error("新增互联站点价格失败", e);
                }

            }
        }
        return TResponse.SUCCESS();
    }

    @RequestMapping(value = "/hlhtTenantOpenPile", method = RequestMethod.GET)
    public String hlhtTenantOpenPile(@RequestParam("tenantOpenId") Long tenantOpenId,
                                     @RequestParam("elecStationId") Long elecStationId,
                                     Model model) {
        HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(tenantOpenId);
        model.addAttribute("tenantOpenId", tenantOpenId);
        model.addAttribute("hlhtTenantCode", hlhtTenantOpen.getHlhtTenantCode());
        model.addAttribute("hlhtOperatorId", hlhtTenantOpen.getHlhtOperatorId());
        model.addAttribute("hlhtOpenType", hlhtTenantOpen.getHlhtOpenType());
        model.addAttribute("elecStationId", elecStationId);
        return "/templates/hlht/hlhtTenantOpenPile";
    }


    /**
     * 跳转设置前端互联控制页面(反向)
     *
     * @return
     */
    @RequestMapping(value = "/stationOpenfor/{tenantCode}/{hlhtOperatorId}", method = RequestMethod.GET)
    public String stationOpenfor(@PathVariable("tenantCode") String tenantCode,
                                 @PathVariable("hlhtOperatorId") String hlhtOperatorId,
                                 Model model) {
        TenantSetting tenantSetting = tenantSettingService.selectByPrimaryKey(tenantCode);
        HlhtOperatorSetting hlhtOperatorSetting = hlhtOperatorSettingService.selectByTenantCodeAndHlhtOperatorId(tenantCode, hlhtOperatorId);
        model.addAttribute("hlhtOperatorSetting", hlhtOperatorSetting);
        model.addAttribute("tenantSetting", tenantSetting);
        return "/templates/hlht/stationOpenfor";
    }


    /**
     * 更新前端互联控制
     */
    @RequestMapping(value = "/updateStationOpenfor", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateStationOpenfor(HlhtOperatorSetting hlhtOperatorSetting) {
        try {
            hlhtOperatorSetting.setLastUpdatedTime(new Date());
            hlhtOperatorSettingService.updateByPrimaryKeySelective(hlhtOperatorSetting);
            return TResponse.SUCCESS("操作成功");
        } catch (Exception e) {
            logger.error("操作失败", e);
            return TResponse.FAIL("操作失败");
        }
    }
}

